package edu.hnu.cloudflightserver.util;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import com.mchange.v2.c3p0.ComboPooledDataSource;

public class DataBaseUtil {

	@SuppressWarnings("rawtypes")
	private static Class clz = DataBaseUtil.class;

	private static ComboPooledDataSource connectionsPool;

	private static boolean isBAE = false;

	public static void initConnectionPool() {
		Properties pro = FileUtil.getPropertiesAtClassPath(CommonUtil.SQL_CONFIG_FILE_NAME);
		if (pro == null) {
			LogUtil.warn(clz, "initConnectionPool", "load database.Properties error");
			return;
		}
		if (connectionsPool == null) {
			if (isBAE) {
				connectionsPool = new ComboPooledDataSource();

				/*
				 * String BAEurl =
				 * "jdbc:mysql://"+BaeEnv.getBaeHeader(BaeEnv.BAE_ENV_ADDR_SQL_IP
				 * )+":"+BaeEnv.getBaeHeader(BaeEnv.BAE_ENV_ADDR_SQL_PORT)+"/"+
				 * "FSPVpKLKvqvCVAdXOiMi";
				 * connectionsPool.setDriverClass(pro.getProperty
				 * ("c3p0.driverClass"));
				 * connectionsPool.setUser(pro.getProperty
				 * (BaeEnv.getBaeHeader(BaeEnv.BAE_ENV_AK));
				 * connectionsPool.setPassword
				 * (BaeEnv.getBaeHeader(BaeEnv.BAE_ENV_SK));
				 * connectionsPool.setJdbcUrl(pro.getProperty(BAEurl));
				 */
				connectionsPool.setInitialPoolSize(Integer.valueOf(pro.getProperty("c3p0.initialPoolSize")));
				connectionsPool.setAcquireIncrement(Integer.valueOf(pro.getProperty("c3p0.acquireIncrement")));
				connectionsPool.setMinPoolSize(Integer.valueOf(pro.getProperty("c3p0.minPoolSize")));
				connectionsPool.setMaxPoolSize(Integer.valueOf(pro.getProperty("c3p0.maxPoolSize")));
				connectionsPool.setMaxIdleTimeExcessConnections(Integer.valueOf(pro.getProperty("c3p0.maxIdleTimeExcessConnections")));

			} else {
				connectionsPool = new ComboPooledDataSource();
				try {
					connectionsPool.setDriverClass(pro.getProperty("c3p0.driverClass"));
					connectionsPool.setUser(pro.getProperty("c3p0.user"));
					connectionsPool.setPassword(pro.getProperty("c3p0.password"));
					connectionsPool.setJdbcUrl(pro.getProperty("c3p0.jdbcUrl"));
					connectionsPool.setInitialPoolSize(Integer.valueOf(pro.getProperty("c3p0.initialPoolSize")));
					connectionsPool.setAcquireIncrement(Integer.valueOf(pro.getProperty("c3p0.acquireIncrement")));
					connectionsPool.setMinPoolSize(Integer.valueOf(pro.getProperty("c3p0.minPoolSize")));
					connectionsPool.setMaxPoolSize(Integer.valueOf(pro.getProperty("c3p0.maxPoolSize")));
					connectionsPool.setMaxIdleTimeExcessConnections(Integer.valueOf(pro.getProperty("c3p0.maxIdleTimeExcessConnections")));
				} catch (PropertyVetoException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static Connection getConnection() {
		try {
			return connectionsPool.getConnection();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public static void releaseConnection(Connection conn, Statement st, ResultSet rs) {
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (st != null) {
			try {
				st.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
